package cn.peach.hrm.service.impl;

import cn.peach.hrm.constants.BaseConstants;
import cn.peach.hrm.domain.Login;
import cn.peach.hrm.mapper.LoginMapper;
import cn.peach.hrm.service.ILoginService;
import cn.peach.hrm.service.IMealService;
import cn.peach.hrm.util.AssertUtil;
import cn.peach.hrm.util.encrypt.HttpUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author peach.ja
 * @since 2021-11-24
 */
@Service
@Slf4j
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    /**
     * 密码模式获取token
     */
    @Value("{oauth2.url.token-password}")
    private String urlTokenPassword;

    @Autowired
    private IMealService mealService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 重写父类的insert方法
     * @param login
     * @return
     */
    @Override
    public boolean insert(Login login) {
        login.setPassword(passwordEncoder.encode(login.getPassword()));
        return super.insert(login);
    }

    @Override
    public Map<String, String> login(Login login) {
        AssertUtil.isNotEmpty(login.getUsername(),"用户名不能为空");
        AssertUtil.isNotEmpty(login.getPassword(),"密码不能为空");
        /**
         * 查询用户
         */
        Wrapper<Login> wrapper = new EntityWrapper<>();
        wrapper.eq("username", login.getUsername());
        Login loginOne = super.selectOne(wrapper);
        AssertUtil.isNotNull(loginOne, "无效用户名");
        // TODO
        String clientId = "";
        String clientSecret = "123";
        if(loginOne.getType().intValue() == BaseConstants.Login.TYPE_SYSTEM){
            // 后台用户
            clientId = "admin";
        }else{
            // 前台用户
            clientId = "website";
        }
        String completeURL = String.format(urlTokenPassword, clientId,clientSecret,login.getUsername(),login.getPassword());
        log.info("拼接获取TokenURL ->{}", completeURL);

        Map<String, String> map = HttpUtil.sendPost(completeURL);
        return map;
    }

    /**
     * 后台使用
     * @param login
     * @param mealId
     * @return
     */
    @Override
    @Transactional
    public Login saveLoginAndMeal(Login login, Long mealId) {

        if (StringUtils.isEmpty(login.getUsername())) {
            throw new RuntimeException("用户名不饿能为空");
        }
        if (StringUtils.isEmpty(login.getPassword())) {
            throw new RuntimeException("请输入密码");
        }
        if (mealId == null) {
            throw new RuntimeException("请选择套餐");
        }

        login.setEnabled(true);
        login.setPassword(passwordEncoder.encode(login.getPassword()));
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        insert(login);
        //保存套餐
        Long loginId = login.getId();
        mealService.saveRelationWithLogin(mealId ,loginId );
        return login;
    }
}
